package com.test.dababy.dababy_04_home;

import static com.test.dababy.dababy_00_utils.DababyConfigs.Debug;
import static com.test.dababy.dababy_00_utils.DababyConfigs.PermissionCode_Camera;
import static com.test.dababy.dababy_00_utils.DababyConfigs.PermissionCode_Home;
import static com.test.dababy.dababy_00_utils.DababyConfigs.St_dababyate_Wait;
import static com.test.dababy.dababy_00_utils.DababyConfigs.State_Fa_dababyil;
import static com.test.dababy.dababy_00_utils.DababyConfigs.State_R_dababyp;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.test.dababy.R;
import com.test.dababy.dababy_00_utils.DababyUtil;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_07_loan_status.BaseLoanFragment;
import com.test.dababy.dababy_05_process.Pro_dababycessIn_dababyfo;
import com.test.dababy.dababy_08_loan_repay.Ln_dababyRpyFr_dababyagment;
import com.test.dababy.dababy_07_loan_status.LoanStat_dababyeFailFrag_dababyment;
import com.test.dababy.dababy_07_loan_status.LoanSta_dababyteWaitFra_dababygment;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_10_vip.Pers_dababyonalIn_dababyfo;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainAct_dababyivity extends AppCompatActivity implements EasyPermissions.RationaleCallbacks,EasyPermissions.PermissionCallbacks{


    private String uiyher8 ="";
    private char hlikrj3 ='4';
    private double haerrt4 =0.9819073579725874;
    private double hrjh68 =0.8989391823404798;
    private boolean jslror1 =false;
    private int havmb3 =298;
    private String hnsero32 ="";
    private int jlkrt8 =168;
    private String hasl23 ="";
    public void hjrtle3(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getUiyher8()))
                    setHnsero32(String.valueOf(getHlikrj3())+ getHaerrt4()+ getHrjh68()+ isJslror1());
                else setHasl23(String.valueOf(getHavmb3())+ getJlkrt8());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
    public void setUiyher8(String uiyher8) {this.uiyher8 = uiyher8;}
    public void setHlikrj3(char hlikrj3) {this.hlikrj3 = hlikrj3;}
    public void setHaerrt4(double haerrt4) {this.haerrt4 = haerrt4;}
    public void setHrjh68(double hrjh68) {this.hrjh68 = hrjh68;}
    public void setJslror1(boolean jslror1) {this.jslror1 = jslror1;}
    public void setHavmb3(int havmb3) {this.havmb3 = havmb3;}
    public void setHnsero32(String hnsero32) {this.hnsero32 = hnsero32;}
    public void setJlkrt8(int jlkrt8) {this.jlkrt8 = jlkrt8;}
    public void setHasl23(String hasl23) {this.hasl23 = hasl23;}
    public String getUiyher8() {return uiyher8;}
    public char getHlikrj3() {return hlikrj3;}
    public double getHaerrt4() {return haerrt4;}
    public double getHrjh68() {return hrjh68;}
    public boolean isJslror1() {return jslror1;}
    public int getHavmb3() {return havmb3;}
    public String getHnsero32() {return hnsero32;}
    public int getJlkrt8() {return jlkrt8;}
    public String getHasl23() {return hasl23;}




    private View dsView,homeView,pcView;
    private ImageView dsIv,homeIv,pcIv;
    private TextView dsTv,homeTv,pcTv;

    private BaseLoanFragment baseLnF_dababyragment;
    private PersonalCenterF_dababyragment pcFra_dababygment;
    private DataSaf_dababyetyFragment dsFragm_dababyent;

    private int page_dababyFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dababy_main);
        if (!Debug) getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        dsView = findViewById(R.id.adm_view_ds);
        homeView = findViewById(R.id.adm_view_home);
        pcView = findViewById(R.id.adm_view_pc);
        dsIv = findViewById(R.id.adm_iv_ds);
        homeIv = findViewById(R.id.adm_iv_home);
        pcIv = findViewById(R.id.adm_iv_pc);
        dsTv = findViewById(R.id.adm_tv_ds);
        homeTv = findViewById(R.id.adm_tv_home);
        pcTv = findViewById(R.id.adm_tv_pc);

        in_dababyit();
    }

    private void in_dababyit(){

        pcFra_dababygment = new PersonalCenterF_dababyragment();
        dsFragm_dababyent = new DataSaf_dababyetyFragment();

        pcView.setOnClickListener(v -> toM_dababye());
        dsView.setOnClickListener(v -> to_dababyDs());
        homeView.setOnClickListener(v -> to_dababyLn());

        hjrtle3();

        //初始化用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction
                .add(R.id.adm_layout_fragment, dsFragm_dababyent)
                .add(R.id.adm_layout_fragment, pcFra_dababygment)
                .commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("MAIN","START","");
        updateAkoma_dababypaLendUtil();
    }

    /**
     * 判断用户流程状态，如果状态有改变再更新页面
     */
    private void updateAkoma_dababypaLendUtil(){
        //判断用户流程状态
        String state = Pro_dababycessIn_dababyfo.getInstance_dababy().getDababyUtil();
        if (DababyUtil.isStateChange_dababy(state)) updatePa_dababygeState(state);
    }

    private void updatePa_dababygeState(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (baseLnF_dababyragment != null) fragmentTransaction.remove(baseLnF_dababyragment);

        switch (userState){
            case State_R_dababyp://还款状态
                baseLnF_dababyragment = new Ln_dababyRpyFr_dababyagment();
                Pers_dababyonalIn_dababyfo.getInstance().setRepayFlag(true);
                Pers_dababyonalIn_dababyfo.getInstance().uploadUserInfo(null);
                break;
            case St_dababyate_Wait://审核/放款中状态
                baseLnF_dababyragment = new LoanSta_dababyteWaitFra_dababygment();
                break;
            case State_Fa_dababyil://被拒状态
                baseLnF_dababyragment = new LoanStat_dababyeFailFrag_dababyment();
                break;
            default:
                baseLnF_dababyragment = new HomeFr_dababyagment();
                if (Pers_dababyonalIn_dababyfo.getInstance().getRepayFlag()){
                    Pers_dababyonalIn_dababyfo.getInstance().setRepayFlag(false);
                    //经验+12
                    Pers_dababyonalIn_dababyfo.getInstance().addExpValue(12);
                    Pers_dababyonalIn_dababyfo.getInstance().uploadUserInfo(null);
                }
        }

        fragmentTransaction.add(R.id.adm_layout_fragment, baseLnF_dababyragment);

        switch (page_dababyFlag) {
            case 1:
                fragmentTransaction.show(dsFragm_dababyent).hide(baseLnF_dababyragment).hide(pcFra_dababygment);
                break;
            case 2:
                fragmentTransaction.show(pcFra_dababygment).hide(baseLnF_dababyragment).hide(dsFragm_dababyent);
                break;
            default:
                fragmentTransaction.show(baseLnF_dababyragment).hide(dsFragm_dababyent).hide(pcFra_dababygment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void to_dababyDs(){

        OkHttpUtil.getInstance_dababy().uploadEventPoint("MAIN","DATA_SAFETY_CLICK","");

        page_dababyFlag = 1;

        dsIv.setBackgroundResource(R.drawable.icon_ds_light);
        dsTv.setTextColor(getColor(R.color.tab_light));

        homeIv.setBackgroundResource(R.drawable.icon_pc_gray);
        homeTv.setTextColor(getColor(R.color.tab_gray));

        pcIv.setBackgroundResource(R.drawable.icon_pc_gray);
        pcTv.setTextColor(getColor(R.color.tab_gray));

        getSupportFragmentManager().beginTransaction()
                .show(dsFragm_dababyent)
                .hide(baseLnF_dababyragment)
                .hide(pcFra_dababygment)
                .commitAllowingStateLoss();

    }

    private void to_dababyLn(){

        OkHttpUtil.getInstance_dababy().uploadEventPoint("MAIN","HOME_CLICK","");

        page_dababyFlag = 0;

        dsIv.setBackgroundResource(R.drawable.icon_ds_gray);
        dsTv.setTextColor(getColor(R.color.tab_gray));

        homeIv.setBackgroundResource(R.drawable.icon_pc_light);
        homeTv.setTextColor(getColor(R.color.tab_light));

        pcIv.setBackgroundResource(R.drawable.icon_pc_gray);
        pcTv.setTextColor(getColor(R.color.tab_gray));

        getSupportFragmentManager().beginTransaction()
                .show(baseLnF_dababyragment)
                .hide(dsFragm_dababyent)
                .hide(pcFra_dababygment)
                .commitAllowingStateLoss();

    }

    private void toM_dababye(){

        OkHttpUtil.getInstance_dababy().uploadEventPoint("MAIN","ME_CLICK","");

        page_dababyFlag = 2;

        dsIv.setBackgroundResource(R.drawable.icon_ds_gray);
        dsTv.setTextColor(getColor(R.color.tab_gray));

        homeIv.setBackgroundResource(R.drawable.icon_pc_gray);
        homeTv.setTextColor(getColor(R.color.tab_gray));

        pcIv.setBackgroundResource(R.drawable.icon_pc_light);
        pcTv.setTextColor(getColor(R.color.tab_light));

        getSupportFragmentManager().beginTransaction()
                .show(pcFra_dababygment)
                .hide(dsFragm_dababyent)
                .hide(baseLnF_dababyragment)
                .commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DababyUtil.clearState_dababy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("MAIN","END","");
    }


    //---------------------权限处理--------------------

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode==PermissionCode_Camera && pcFra_dababygment!=null) pcFra_dababygment.onCameraPermissionGranted();
        else if (requestCode==PermissionCode_Home && baseLnF_dababyragment!=null && baseLnF_dababyragment instanceof HomeFr_dababyagment)
            ((HomeFr_dababyagment)baseLnF_dababyragment).onPermissionsGranted();

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PermissionCode_Camera || requestCode == PermissionCode_Home) {
            if (EasyPermissions.somePermissionPermanentlyDenied(this, perms))
                new AppSettingsDialog.Builder(this).build().show();
        }
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (requestCode == PermissionCode_Camera)
            EasyPermissions.requestPermissions(this,
                    getString(R.string.launch_permissions_camera_dababy),
                    PermissionCode_Camera,
                    Manifest.permission.CAMERA);
        else if (requestCode == PermissionCode_Home)
            EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions_all_dababy),
                    PermissionCode_Home, App_dababyShow_dababyCustomize.getInstance_dababy().getHomePermission_dababy());
    }

    @Override
    public void onRationaleDenied(int requestCode) {}


}